package saucaca.Adapter;

import java.util.ArrayList;
import java.util.List;

import saucaca.Download.DownloadTask;
import saucaca.Object.RingtoneObject;
import saucaca.Object.SongObject;
import saucaca.Util.AppController;
import saucaca.Util.PreferenceUtils;
import saucaca.Util.TagName;
import saucaca.fragment.AccountLoginFragment;
import saucaca.fragment.RingToneListSong;
import saucaca.fragment.SelectPlaylistDialogFragment;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.toolbox.ImageLoader;
import com.lemon.kenhnhacvn.BaseActivity;
import com.lemon.kenhnhacvn.BaseEngine;
import com.lemon.kenhnhacvn.MainActivity;
import com.lemon.kenhnhacvn.R;
@SuppressLint("InflateParams")
public class ExpandableSongAdapter2 extends BaseExpandableListAdapter {
	private int[] groupStatus;
	ArrayList<SongObject> listSongs;
	ExpandableListView mExpandableListView;
	Context mContext;
	ImageLoader imageLoader;
	boolean isHome = false;
	boolean isBXH = false;
	public ExpandableSongAdapter2(Context pContext, ExpandableListView pExpandableListView, List<SongObject> listSongs) {
		this.mContext = pContext;
		this.mExpandableListView = pExpandableListView;
		mExpandableListView.setGroupIndicator(mContext.getResources().getDrawable(R.drawable.expandlist_indicator));
		this.listSongs = (ArrayList<SongObject>) listSongs;
		groupStatus = new int[this.listSongs.size()];
		setListclick();
		setListEvent();
	}
	public ExpandableSongAdapter2(Context pContext, ExpandableListView pExpandableListView, List<SongObject> listSongs, boolean isHome) {
		this.mContext = pContext;
		this.mExpandableListView = pExpandableListView;
		mExpandableListView.setGroupIndicator(mContext.getResources().getDrawable(R.drawable.expandlist_indicator));
		this.listSongs = (ArrayList<SongObject>) listSongs;
		groupStatus = new int[this.listSongs.size()];
		setListclick();
		setListEvent();
		this.isHome = isHome;
	}
	
	public ExpandableSongAdapter2(Context pContext, ExpandableListView pExpandableListView, List<SongObject> listSongs, boolean isHome,boolean isBXH) {
		this.mContext = pContext;
		this.mExpandableListView = pExpandableListView;
		mExpandableListView.setGroupIndicator(mContext.getResources().getDrawable(R.drawable.expandlist_indicator));
		this.listSongs = (ArrayList<SongObject>) listSongs;
		groupStatus = new int[this.listSongs.size()];
		setListclick();
		setListEvent();
		this.isHome = isHome;
		this.isBXH=isBXH;
	}

	public void update(ArrayList<SongObject> listSongs) {
		this.listSongs=listSongs;
		groupStatus = new int[this.listSongs.size()];
		notifyDataSetChanged();
	}
	private void setListclick() {
		mExpandableListView.setOnGroupClickListener(new OnGroupClickListener() {
			@Override
			public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
				{
					((MainActivity) mContext).showPanelControl(true);
					if (listSongs.get(groupPosition).getPATCH_FULL() != null && !listSongs.get(groupPosition).getPATCH_FULL().equals("")) {
						((MainActivity) mContext).SendPlaylistAndplay(listSongs, groupPosition);
					}
					return true;
				}
			}
		});
	}

	public void expandGroup(int groupPosition) {
		if (mExpandableListView.isGroupExpanded(groupPosition))
			mExpandableListView.collapseGroup(groupPosition);
		else
			mExpandableListView.expandGroup(groupPosition);
	}

	private void setListEvent() {
		mExpandableListView.setOnGroupExpandListener(new OnGroupExpandListener() {
			@Override
			public void onGroupExpand(int arg0) {
				groupStatus[arg0] = 1;
				int len = listSongs.size();
				for (int i = 0; i < len; i++) {
					if (i != arg0) {
						mExpandableListView.collapseGroup(i);
					}
				}
			}
		});
		mExpandableListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {
			@Override
			public void onGroupCollapse(int arg0) {
				// TODO Auto-generated method stub
				groupStatus[arg0] = 0;
			}
		});
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return 1;
	}

	@Override
	public Object getGroup(int arg0) {
		return listSongs.get(arg0);
	}

	@Override
	public int getGroupCount() {
		return listSongs.size();
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return null;
	}

	@Override
	public long getGroupId(int groupPosition) {
		return 0;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return 0;
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@SuppressLint("InflateParams") @Override
	public View getGroupView(int groupPosition, boolean isExpanded, View convertView, final ViewGroup parent) {
		GroupHolder groupHolder;
		if (convertView == null) {
			if (isHome) {
				convertView = LayoutInflater.from(mContext).inflate(R.layout.item_song_online_home, null);
				groupHolder = new GroupHolder();
				groupHolder.backgroud = (RelativeLayout) convertView.findViewById(R.id.relativeGroup);
				groupHolder.artist = (TextView) convertView.findViewById(R.id.custom_baihat_item_artist);
				groupHolder.listent = (TextView) convertView.findViewById(R.id.custom_baihat_item_textluotnghe);
				groupHolder.title = (TextView) convertView.findViewById(R.id.custom_baihat_item_title);
				groupHolder.img = (ImageView) convertView.findViewById(R.id.custom_groupview_imageExpand);
				groupHolder.like = (TextView) convertView.findViewById(R.id.custom_baihat_item_textYeuthich);
			} else {
				convertView = LayoutInflater.from(mContext).inflate(R.layout.item_song_online, null);
				groupHolder = new GroupHolder();
				groupHolder.relati_image  = (RelativeLayout) convertView.findViewById(R.id.item_bxh_relativeRank);
				groupHolder.rank  = (RelativeLayout) convertView.findViewById(R.id.item_bxh_relativeRank_icon);
				groupHolder.txtRank = (TextView) convertView.findViewById(R.id.item_bxh_textViewRank);
				groupHolder.imgRank = (ImageView) convertView.findViewById(R.id.item_bxh_imgRankColor);
				groupHolder.backgroud = (RelativeLayout) convertView.findViewById(R.id.relativeGroup);
				groupHolder.artist = (TextView) convertView.findViewById(R.id.custom_baihat_item_artist);
				groupHolder.listent = (TextView) convertView.findViewById(R.id.custom_baihat_item_textluotnghe);
				groupHolder.title = (TextView) convertView.findViewById(R.id.custom_baihat_item_title);
				groupHolder.img = (ImageView) convertView.findViewById(R.id.custom_groupview_imageExpand);
				groupHolder.imgAvatar = (ImageView) convertView.findViewById(R.id.custom_baihat_item_Avatar);
				groupHolder.like = (TextView) convertView.findViewById(R.id.custom_baihat_item_textYeuthich);
			}
			convertView.setTag(groupHolder);
		} else {
			groupHolder = (GroupHolder) convertView.getTag();
		}
		final int indexgroup=groupPosition;
		if(isBXH){
			if (!listSongs.get(indexgroup).getAVATAR().equals("")){
//				imageLoader.displayImage(listSongs.get(indexgroup).getAVATAR(), groupHolder.imgAvatar);
				try {
					imageLoader=((BaseActivity)mContext).imageLoader;
				} catch (Exception e) {
					imageLoader=AppController.getInstance().getImageLoader();
				}
				
				imageLoader.get(listSongs.get(groupPosition).getAVATAR(), ImageLoader.getImageListener(groupHolder.imgAvatar,
			         R.drawable.ic_default, R.drawable.ic_default));
			}
			groupHolder.relati_image.setVisibility(View.VISIBLE);
			groupHolder.rank.setVisibility(View.VISIBLE);
			groupHolder.txtRank.setText(Integer.toString((indexgroup + 1)));
			switch (indexgroup) {
			case 0:
				groupHolder.imgRank.setImageResource(R.drawable.bxh_icon1);
				break;
			case 1:
				groupHolder.imgRank.setImageResource(R.drawable.bxh_icon2);
				break;
			case 2:
				groupHolder.imgRank.setImageResource(R.drawable.bxh_icon3);
				break;
			default:
				groupHolder.imgRank.setImageResource(R.drawable.bxh_icon4);
				break;
			}
		}
			if (groupStatus[indexgroup] == 0) {
				groupHolder.img.setImageResource(R.drawable.icon_bh_sh2);
			} else {
				groupHolder.img.setImageResource(R.drawable.icon_bh_sh);
			}
		groupHolder.img.setOnClickListener(new View.OnClickListener() {
			@SuppressLint("NewApi")
			@Override
			public void onClick(View v) {
				expandGroup(indexgroup);
			}
		});
		groupHolder.like.setText("0");
		groupHolder.artist.setText(listSongs.get(indexgroup).getNAME_SINGERS());
		groupHolder.listent.setText(BaseEngine.convertStringLongtoInteger(listSongs.get(indexgroup).getISVIEW()));
		groupHolder.title.setText(listSongs.get(indexgroup).getTXNAME());
		return convertView;
	}

	@Override
	public View getChildView(final int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = LayoutInflater.from(mContext).inflate(R.layout.item_song_online_child_expand, null);
		}
		LinearLayout buttonAddplaylist = (LinearLayout) convertView.findViewById(R.id.custom_baihat_home_buttonAddplaylist);
		LinearLayout buttonDownload = (LinearLayout) convertView.findViewById(R.id.custom_baihat_home_buttonDownload);
		LinearLayout buttonRington = (LinearLayout) convertView.findViewById(R.id.custom_baihat_home_buttonRington);
		LinearLayout buttonShare = (LinearLayout) convertView.findViewById(R.id.custom_baihat_home_buttonShare);
		final int indexGroup = groupPosition;
		buttonAddplaylist.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String s = PreferenceUtils.getString(mContext, TagName.KeyPreference_user);
				if (s != null && !s.equals("")) {
					DialogFragment df = null;
					df = SelectPlaylistDialogFragment.newInstance(listSongs.get(groupPosition).getSONG_ID());
					df.show(((MainActivity) mContext).getSupportFragmentManager(), null);
				} else {
					new AlertDialog.Builder(mContext).setTitle("Bạn chưa đăng nhập!").setMessage("Hãy đăng nhập để add playlist!")
							.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog, int which) {
									((MainActivity) mContext).switchContent(AccountLoginFragment.class.getName());
								}
							}).setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog, int which) {
									// do nothing
								}
							}).setIcon(android.R.drawable.ic_dialog_alert).show();
				}
			}
		});

		buttonDownload.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				DownloadTask downloadTask = new DownloadTask(mContext,listSongs.get(indexGroup) );
				downloadTask.startDownload();
			}
		});
		buttonRington.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(!BaseEngine.convertStringLongtoInteger(listSongs.get(indexGroup).getISTONE()).equals("0")){
					RingtoneObject ringtoneObject=new RingtoneObject();
					ringtoneObject.setSONG_ID(listSongs.get(indexGroup).getSONG_ID());
					ringtoneObject.setTXNAME(listSongs.get(indexGroup).getTXNAME());
					((MainActivity) mContext).switchContent(RingToneListSong.class.getName(), ringtoneObject);
				}else Toast.makeText(mContext, "Đang cập nhật", Toast.LENGTH_SHORT).show();
			}
		});
		buttonShare.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String name=listSongs.get(indexGroup).getTXNAME();
				String link=listSongs.get(indexGroup).getTXURL();
				TagName.showLogError("Share", "name: "+name);
				TagName.showLogError("Share", "link: "+link);
				BaseEngine.shareTextUrl(mContext, name, link);
			}
		});

		return convertView;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return false;
	}

	class GroupHolder {
		boolean isExpanded = false;
		RelativeLayout backgroud,rank,relati_image;
		TextView txtRank;
		ImageView imgRank;
		ImageView img;
		ImageView imgAvatar;
		TextView title;
		TextView artist;
		TextView listent;
		TextView like;
	}

	class ChildHolder {
		ImageButton buttonAddplaylist;
		ImageButton buttonDownload;
		ImageButton buttonRington;
		ImageButton buttonShare;
	}

}
